import {Component, OnInit, Input, OnChanges, ViewChild} from '@angular/core';
import {NzMessageService} from "ng-zorro-antd";
import {FetchService} from "../../../core/net/fetch.service";
import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser";
import {format} from "date-fns";
import {NzModalRef} from "ng-zorro-antd/modal";

@Component({
  selector: "app-jrsq-big-carousel",
  templateUrl: "./jrsq-big-carousel.component.html",
  styleUrls: ["./jrsq-big-carousel.component.scss"]
})
export class JrsqBigCarouselComponent implements OnInit {
  array = [1, 2, 3, 4];
  jrsqData = [];
  ggjdData = {};
  waterData = {};

  warningList: any[] = [];

  ggjdModalShow = false;
  ggjdReportSrc: SafeResourceUrl;
  ggjdReportType = 1;

  jrsqModalShow = false;
  jrsqReportSrc: SafeResourceUrl;
  jrsqType = 1;
  tplModal: NzModalRef;
  sIndex = 0;
  isUpdate = false;
  upArr = [];
  updateData = [];

  data = [
    [
      {id: 1, name: '灌区总引水', flow: 400},
      {id: 2, name: '河西总干渠', flow: 400},
      {id: 3, name: '河东总干渠', flow: 400},
    ],
    [
      {id: 4, name: '唐徕渠', flow: 1},
      {id: 5, name: '西干渠', flow: 2},
      {id: 6, name: '惠农渠', flow: 3},
      {id: 7, name: '汉延渠', flow: 4},
      {id: 8, name: '大清渠', flow: 5},
      {id: 9, name: '泰民渠', flow: 6},
    ],
    [
      {id: 10, name: '秦渠', flow: 10},
      {id: 11, name: '汉渠', flow: 11},
      {id: 12, name: '马连渠', flow: 12},
      {id: 13, name: '东干渠', flow: 13},
    ],
    [
      {id: 14, name: '七星渠', flow: 14},
      {id: 15, name: '跃进渠', flow: 15},
      {id: 16, name: '北干渠', flow: 16},
      {id: 17, name: '角渠', flow: 17},
      {id: 18, name: '寿渠', flow: 18},
    ],
    [
      {id: 19, name: '泉眼山', flow: 14},
      {id: 20, name: '大战场', flow: 15},
      {id: 21, name: '固扩', flow: 16},
      {id: 22, name: '水源泵站', flow: 17},
      {id: 23, name: '红寺堡', flow: 18},
      {id: 23, name: '盐环定', flow: 18},
    ]
  ];

  constructor(
    private message: NzMessageService,
    private fetchService: FetchService,
    private sanitizer: DomSanitizer
  ) {
  }

  ngOnInit() {
    this.queryInfo();
    this.queryGqjjReport();
    this.queryJrsqReport();
    this.testGdxg();
  }

  testGdxg(): void {
    setInterval(() => {
      /*this.sIndex = (this.sIndex + 1) % 5;
      console.log(this.sIndex);*/
      console.log('数据要更新啦');
      this.updateData = [
        {
          "name": "灌区总引水",
          "id": 1,
          "flow": Math.floor((Math.random() * 100) + 1),
          "children": [
            {
              "name": "河西总干渠",
              "id": 2,
              "flow": 10
            },
            {
              "name": "河东总干渠",
              "id": 3,
              "flow": 10
            }
          ]
        },
        {
          "children": [
            {
              "name": "西干渠",
              "id": 6,
              "flow": Math.floor((Math.random() * 100) + 1),
            },
            {
              "name": "大清渠",
              "id": 9,
              "flow": 8.86
            },
            {
              "name": "汉延渠",
              "id": 8,
              "flow": 30.5
            },
            {
              "name": "泰民渠",
              "id": 10,
              "flow": 4.12
            },
            {
              "name": "唐徕渠",
              "id": 5,
              "flow": 79.6
            },
            {
              "name": "惠农渠",
              "id": 7,
              "flow": 87.9
            }
          ]
        },
        {
          "children": [
            {
              "name": "汉渠",
              "id": 13,
              "flow": 15.1
            },
            {
              "name": "马莲渠",
              "id": 14,
              "flow": 0.0
            },
            {
              "name": "秦渠",
              "id": 12,
              "flow": 50.4
            },
            {
              "name": "东干渠",
              "id": 15,
              "flow": 48.9
            }
          ]
        },
        {
          "children": [
            {
              "name": "北干渠",
              "id": 19,
              "flow": 42.0
            },
            {
              "name": "角渠",
              "id": 20,
              "flow": 1.36
            },
            {
              "name": "跃进渠",
              "id": 18,
              "flow": 18.9
            },
            {
              "name": "寿渠",
              "id": 21,
              "flow": 10.6
            },
            {
              "name": "七星渠",
              "id": 17,
              "flow": 56.7
            }
          ]
        },
        {
          "name": null,
          "id": 22,
          "flow": 0,
          "children": [
            {
              "name": "泉眼山泵站",
              "id": 54,
              "flow": 21.879
            },
            {
              "name": "大战场泵站",
              "id": 67,
              "flow": 9.118
            },
            {
              "name": "盐环定一泵站",
              "id": 9,
              "flow": 10.359
            },
            {
              "name": "固扩",
              "id": 25,
              "flow": 6
            },
            {
              "name": "水源泵站",
              "id": 26,
              "flow": 10
            },
            {
              "name": "红寺堡",
              "id": 27,
              "flow": 0
            }
          ]
        }
      ];
      this.upArr = this.convertToArr(this.updateData);
      this.jrsqData.map(item => {
        if (item.name) {
          // 查询改记录的新值
          const newItem = this.upArr.filter(i => i.id == item.id)[0];
          // 更新新值
          item.flow = item.newflow;
          item.newflow = newItem.flow;
        }
        if (item.children.length > 0) {
          item.children.map(child => {
            // 查询改记录的新值
            const newItem = this.upArr.filter(i => i.id == child.id)[0];
            child.flow = child.newflow;
            child.newflow = newItem.flow;
          });
        }
      });
      // 播放完动画就保持新值和老值一致
      setTimeout(() => {
        this.jrsqData.map(item => {
          if (item.name) {
            item.flow = item.newflow;
          }
          if (item.children.length > 0) {
            item.children.map(child => {
              child.flow = child.newflow;
            });
          }
        });
        console.log(this.jrsqData);
      }, 5000);
    }, 10000);
  }

  convertToArr(data): any {
    const arr = [];
    data.map(item => {
      if (item.name) {
        arr.push({name: item.name, id: item.id, flow: item.flow});
      }
      if (item.children.length > 0) {
        item.children.map(child => {
          arr.push({name: child.name, id: child.id, flow: child.flow});
        });
      }
    });
    return arr;
  }

  queryInfo(): void {
    // 今日水情
    this.fetchService.post<any>('muw/wrsMuwWatReg/list', {}).subscribe(res => {
      res = [
        {
          "name": "灌区总引水",
          "id": 1,
          "flow": 538,
          "children": [
            {
              "name": "河西总干渠",
              "id": 2,
              "flow": 10
            },
            {
              "name": "河东总干渠",
              "id": 3,
              "flow": 10
            }
          ]
        },
        {
          "children": [
            {
              "name": "西干渠",
              "id": 6,
              "flow": 38.0
            },
            {
              "name": "大清渠",
              "id": 9,
              "flow": 8.86
            },
            {
              "name": "汉延渠",
              "id": 8,
              "flow": 30.5
            },
            {
              "name": "泰民渠",
              "id": 10,
              "flow": 4.12
            },
            {
              "name": "唐徕渠",
              "id": 5,
              "flow": 79.6
            },
            {
              "name": "惠农渠",
              "id": 7,
              "flow": 87.9
            }
          ]
        },
        {
          "children": [
            {
              "name": "汉渠",
              "id": 13,
              "flow": 15.1
            },
            {
              "name": "马莲渠",
              "id": 14,
              "flow": 0.0
            },
            {
              "name": "秦渠",
              "id": 12,
              "flow": 50.4
            },
            {
              "name": "东干渠",
              "id": 15,
              "flow": 48.9
            }
          ]
        },
        {
          "children": [
            {
              "name": "北干渠",
              "id": 19,
              "flow": 42.0
            },
            {
              "name": "角渠",
              "id": 20,
              "flow": 1.36
            },
            {
              "name": "跃进渠",
              "id": 18,
              "flow": 18.9
            },
            {
              "name": "寿渠",
              "id": 21,
              "flow": 10.6
            },
            {
              "name": "七星渠",
              "id": 17,
              "flow": 56.7
            }
          ]
        },
        {
          "name": null,
          "id": 22,
          "flow": 0,
          "children": [
            {
              "name": "泉眼山泵站",
              "id": 54,
              "flow": 21.879
            },
            {
              "name": "大战场泵站",
              "id": 67,
              "flow": 9.118
            },
            {
              "name": "盐环定一泵站",
              "id": 9,
              "flow": 10.359
            },
            {
              "name": "固扩",
              "id": 25,
              "flow": 6
            },
            {
              "name": "水源泵站",
              "id": 26,
              "flow": 10
            },
            {
              "name": "红寺堡",
              "id": 27,
              "flow": 0
            }
          ]
        }
      ];
      this.jrsqData = res.map((item, i) => {
        if (i > 0) {
          if (item.children.length < 6) {
            item.children.map(child => {
              child.newflow = child.flow;
            });
            item.blank = new Array(6 - item.children.length);
          } else {
            item.blank = [];
          }
        }
        return item;
      });

      this.jrsqData.map(item => {
        if (item.name) {
          item.newflow = item.flow;
        }
        if (item.children.length > 0) {
          item.children.map(child => {
            child.newflow = child.flow;
          });
        }
      });
      console.log(this.jrsqData);

    });

  }


  queryGqjjReport(): void {
    const year = new Date().getFullYear();
    const month = new Date().getMonth() + 1;
    const token = localStorage.token;
    const serverUrl = (window as any).environment.reportUrl;
    let link = '';
    if (this.ggjdReportType == 1) {
      link = `${serverUrl}preview?_u=wrs:实时调度_引水量.ureport.xml&_t=3,6&year=${year}&month=${month}&token=${token}`;
    }
    if (this.ggjdReportType == 2) {
      link = `${serverUrl}preview?_u=wrs:实时调度_市县用水量.ureport.xml&_t=3,6&year=${year}&month=${month}&token=${token}`;
    }
    this.ggjdReportSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }

  queryJrsqReport(): void {
    const token = localStorage.token;
    const date = format(new Date(), 'YYYY-MM-DD');
    const serverUrl = (window as any).environment.reportUrl;
    const link = `${serverUrl}preview?_u=wrs:今日水情.ureport.xml&_t=3,6&date=${date}&token=${token}`;
    this.jrsqReportSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }


}
